﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Types.Database;
using Sio.Mdm.Types;
using Sio.Mdm.BF.Ldap;
using System.Collections.Generic;

namespace Sio.Mdm.Web.Instalacija
{
    public partial class PosteSeznam : Controls.Page
    {
        #region Definitions
        public interface PosteRowEditData
        {
            int PostaId { get; set; }
            int PostnaStevilka { get; set; }
            string Naziv { get; set; }
        }
        #endregion

        #region OnLoad
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            NaloziSeznamPost();
        }
        #endregion

        #region Editing
        protected void UrediPodatke(Object sender, DirectEventArgs e)
        {
            PosteRowEditData EditData = XML.Deserialize<PosteRowEditData>(e.ExtraParams["data"]);
            //vstavljanje novega zapisa
            if (EditData.PostaId == -99)
            {
                PostnaStevilka ent = EntityFactory.CreateInstance<PostnaStevilka>();
                ent.PostnaStevilka = EditData.PostnaStevilka;
                ent.Naziv = EditData.Naziv;
                ent.Update();
            }
            // urejanje obstoječega zapisa
            else if (EditData.PostaId > 0)
            {
                PostnaStevilka ent = EntityFactory.LoadEntity<PostnaStevilka>
                  (new Filter<PostnaStevilka>().Where(r => r.PostaId == EditData.PostaId)
                );

                if (ent.EntityState != EntityState.Unknown)
                {
                    ent.PostnaStevilka = EditData.PostnaStevilka;
                    ent.Naziv = EditData.Naziv;
                    ent.Update();
                }
                else
                {
                    e.Success = false;
                    X.Msg.Alert("Manjkajoči podatki", "Iskani zapis ne obstaja.").Show();
                    return;
                }
            }
        }
        protected void OdstraniZapise(object sender, DirectEventArgs e)
        {
            List<int> deletedItems = e.GetIdList<int>("selected", "PostaId");
            if (deletedItems.Count > 0)
            {
                EntityCollection<PostnaStevilka> col = new EntityCollection<PostnaStevilka>(
                    new Filter<PostnaStevilka>().SelectByField(r => r.PostaId, deletedItems.ToArray())
                );
                col.RemoveAll();
                col.Update();
            }
        }
        #endregion

        #region Private
        private void NaloziSeznamPost()
        {
            GridPosteSeznam.LoadData<PostnaStevilka>(
                new Filter<PostnaStevilka>().Where(r => r.PostaId != null));
        }
        #endregion
    }
}
